package com.example.knowman.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.knowman.ReturnMessage.Message;
import com.example.knowman.entity.rootList;
import com.example.knowman.mapper.RootListMapper;
import com.example.knowman.service.IrootListService;
import org.springframework.stereotype.Service;

@Service
public class rootListServiceImpl extends ServiceImpl<RootListMapper, rootList> implements IrootListService {

    private final RootListMapper rootListMapper;

    public rootListServiceImpl(RootListMapper rootListMapper) {
//        this.iTable1Service = iTable1Service;
        this.rootListMapper = rootListMapper;
    }

    @Override
    public rootList getByUsername(String username) {
        rootList result = this.lambdaQuery().select().eq(rootList::getUsername,username).one();
        return result;
    }

    @Override
    public IPage<rootList> getAll(Integer pageNum, Integer pageSize, String username) {
        IPage<rootList> page = new Page<>(pageNum,pageSize);
        page = this.lambdaQuery().select()
                .like(username !=null && !username.isEmpty(),rootList::getUsername,username).page(page);
        return page;
    }


    @Override
    public Message updateByUsername(String username, String realname, Integer rootType) {
        String tip="";
        Message message = new Message();


        this.lambdaUpdate().eq(rootList::getUsername,username)
                            .set(realname != null && !realname.isEmpty(),rootList::getRealname,realname)
                            .set(rootType != null ,rootList::getRootType,rootType).update();
        message.setCode(200);
        message.setTip("修改成功");


        return  message;

    }

    @Override
    public Message commit(String username, String passwd, String realname, Integer rootType) {
        Message message = new Message();

        rootList rl = new rootList();
        rl.setUsername(username);
        rl.setPasswd(passwd);
        rl.setRootType(rootType);
        rl.setRealname(realname);

        try{
            this.save(rl);
            message.setCode(200);
            message.setTip("新增成功");
        }catch (Exception e){
            message.setCode(100);
            message.setTip("新增失败");
        }

        return message;
    }

    @Override
    public Message deleteByUsername(String username) {
        Message message = new Message();
        QueryWrapper<rootList> wrapper = new QueryWrapper<>();
        wrapper.eq("\"username\"",username);
        try{
            this.remove(wrapper);
            message.setCode(200);
            message.setTip("删除成功");
        }catch (Exception e){
            message.setCode(100);
            message.setTip("删除失败");
        }

        return message;
    }


}
